package com.chlorine.sandbox;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

/**
 * Main activity containing two Fragments: one that receives text input from
 * user (InputFragment) and one that displays all text input in a list
 * (ItemListFragment). This Activity coordinates the interaction between the two
 * Fragments -- it implements an interface from InputFragment, which lets this
 * Activity get the user input, and the interface method calls a
 * ItemListFragment method that updates the list.
 * 
 * @author Caroline Liu
 * 
 */
public class MainActivity extends FragmentActivity implements
		InputFragment.OnItemPostListener {

	/**
	 * Start this Activity, displaying using the layout defined in
	 * activity_main.xml.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// (We don't actually have one of these.)
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Called when user clicks "Add" in InputFragment. Append the newly
	 * submitted text to the list in ItemListFragment.
	 */
	public void onItemPostListener(String s) {
		// Get reference to the ItemListFragment in this Activity.
		ItemListFragment itemListFrg = (ItemListFragment) getSupportFragmentManager()
				.findFragmentById(R.id.frg_list);

		// If we do have a ItemListFragment running, add the new item to its
		// list.
		if (itemListFrg != null) {
			itemListFrg.addItemToList(s);
		} else {
			// We SHOULD have an ItemListFragment already running!
			// See this page for how to start up a new fragment:
			// http://developer.android.com/training/basics/fragments/communicating.html
		}

	}

}
